<script lang="ts" setup>
import { ref } from "vue"
import type { SysRole } from "@/api/user/type/user"
import { roleAddApi } from "@/api/user/role"
import { ElMessage } from "element-plus"

const dialogVisible = ref(false)
const role = ref<SysRole>({} as SysRole)

const openDialog = () => {
  dialogVisible.value = true
}

const onSubmit = () => {
  roleAddApi({
    userRole: role.value
  }).then((res) => {
    const { code, msg } = res
    if (code === 200) {
      ElMessage({
        type: "success",
        message: "新增成功"
      })
    } else {
      ElMessage({
        type: "error",
        message: msg
      })
    }
  })
}

defineExpose({ openDialog })
</script>

<template>
  <el-drawer v-model="dialogVisible" title="I am the title" :with-header="false">
    <el-form :model="role" label-width="auto" style="max-width: 600px">
      <el-form-item label="角色名">
        <el-input v-model="role.role" />
      </el-form-item>
      <el-form-item label="父级">
        <el-input v-model="role.pid" />
      </el-form-item>
      <el-form-item label="描述">
        <el-input v-model="role.spec" maxlength="100" placeholder="请输入描述" type="textarea" show-word-limit />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="onSubmit">提 交</el-button>
      </el-form-item>
    </el-form>
  </el-drawer>
</template>
